﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using FileIndexer.Model;
using FileIndexer.DAL;

namespace FileIndexer.Repository
{
    public class WordRepository
    {
        ObservableCollection<WordModel> w = new ObservableCollection<WordModel>();

        //insertar nuevo registro de palabra
        public void InsertarRegistro(WordModel word)
        {

            
            using (FileIndexerDB_r1Entities entity = new FileIndexerDB_r1Entities())
            {
                var res = (from o in entity.CAT_WORD
                           where o.IdWord == word.IdWord
                           select o).FirstOrDefault();


                if (res == null) 
                {

                    entity.CAT_WORD.AddObject(new CAT_WORD()
                    {
                        Word = word.Word
                    }
                    ); //fin de entity
                      
                
                }
                entity.SaveChanges();
            }

            
        }

        //Generar UNID _id 
        public void InsertarRegistro(WordModel word, bool GenerateId)
        {
            if (GenerateId)
            {
                word.IdWord = (new UNID()).getNewUNID();
            }

            this.InsertarRegistro(word);
            
        }

        public void UpdateRegistro(WordModel word)
        {
            using (var entity = new FileIndexerDB_r1Entities())
            {
                var res = (from o in entity.CAT_WORD
                           where o.IdWord == word.IdWord
                           select o).FirstOrDefault();

                //insertar elemento ya que no existe
                if (res != null)
                {
                    entity.CAT_WORD.AddObject(new CAT_WORD()
                    {

                        Word = word.Word
                    });  
                }
                entity.SaveChanges();
            }           
        }
    }
}
